<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('一般补录页面')" />
    <th:block th:include="include :: viewer-css" />
    <style>
        #manualInput_modal-body{padding-bottom:0px}
        #manualInput_modal-body .box-body{padding:0px}
        #manualInput_modal-body .row>div{padding-right:0px;}
        #manualInput_modal-body .row>div:last-child{padding-right:15px;}
        #imageBox img{width:100%;display:none}
        #sdiImageInput_table tbody td{height:10px;}
        #paeaDsTradeInput_table td{height:10px;}
        .viewer-fullscreen {display: none;}
        .modal-body {padding: 5px 5px 5px 5px;}
        .panel-heading {height: 30px;padding: 1px 15px;}
    </style>
</head>
<body class="white-bg" onKeyDown="doKeyDown(event.keyCode)">
<div id="manualInput_modal-body" class="modal-body">
    <div class="row">
        <div class="col-md-6">
            <div>
                <button type="button" class="btn btn-outline btn-info" onclick="obtainTask('random')" shiro:hasPermission="sdi:manualInput:input">获取随机任务(F1)</button>
                <button type="button" class="btn btn-outline btn-info" onclick="obtainTask('paused')" shiro:hasPermission="sdi:manualInput:input">选择列表任务(F2)</button>
                <button type="button" class="btn btn-outline btn-info" onclick="toggle()">最佳(->)</button>
                <button type="button" class="btn btn-outline btn-info" onclick="zoomIn()">放大(>)</button>
                <button type="button" class="btn btn-outline btn-info" onclick="zoomOut()">缩小(<)</button>
                <button type="button" class="btn btn-outline btn-info" onclick="manualInputWorkLoad()">当日补录量</button>
            </div>
            <div class="panel panel-info" style="margin:5px;">
                <div class="panel-heading">
                    <h4>影像列表 <span id="arcCode"></span></h4>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div id="sdiImageInput" class="col-md-12" style="min-height:300px;max-height:400px;width:100%;overflow-y:auto;overflow-x:hidden">
                            <table id="sdiImageInput-table" data-use-row-attr-func="true"
                                   data-reorderable-rows="true" class="table-dark table-sm"></table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="panel panel-info" style="margin:5px;">
                <div class="panel-heading">
                    <h4>流水列表 <span class="badge badge-primary" id="matchedTradeNum">已勾对：0</span><span class="badge badge-warning" id="unMatchedTradeNum">未勾对：0</span></h4>
                </div>
                <div class="panel-body">
                        <div class="form-group" >
                            <input type="radio" id="radio1" name="tradeFilter" value="0">
                            <label for="radio1">全部流水</label>
                            <input type="radio" id="radio2" name="tradeFilter" value="1" checked>
                            <label for="radio2">未勾对流水</label>
                            <input type="radio" id="radio3" name="tradeFilter" value="2">
                            <label for="radio3">当前影像流水</label>
                            <button type="button" class="btn btn-info btn-xs" onclick="createError()" shiro:hasPermission="sdi:manualInput:input">登记缺票(F5)</button>
                        </div>
                        <div id="paeaDsTradeInput" class="col-md-12" style="min-height:150px;max-height:150px;width:100%;overflow-y:auto;overflow-x:hidden">
                            <table id="paeaDsTradeInput-table" class="table-dark table-sm"></table>
                        </div>
                </div>
            </div>
        </div>
        <div class="col-md-6" style="margin-left:-10px">
            <div class="panel panel-info" style="margin:5px;">
                <div class="panel-heading">
                    <h4>影像信息</h4>
                </div>
                <div class="panel-body">
                    <div id="imageBox" style="height:450px;">
                    </div>
                </div>
            </div>
            <div class="panel panel-info" style="margin:5px;" shiro:hasPermission="sdi:manualInput:input">
                <div class="panel-heading">
                    <h4>补录信息</h4>
                </div>
                <div class="panel-body">
                    <div id="input" class="box-main" style="min-height:80px;max-height:80px;width:100%;overflow-y:auto;overflow-x:hidden">
                        <div class="form-group" >
                            <!--<button type="button" class="btn btn-info btn-xs" onclick="submitInput()">提交补录(XX)</button>-->
                            <button type="button" class="btn btn-info btn-xs" onclick="cancelInput()">取消勾对(XX)</button>
                            <button type="button" class="btn btn-info btn-xs" onclick="signAttachedPage()">标记副票(PgDn)</button>
                            <button type="button" class="btn btn-info btn-xs" onclick="signOtherPage()">标记其他(*)</button>
                            <button type="button" class="btn btn-info btn-xs" onclick="submitBatch('false')">保存批次(F3)</button>
                            <button type="button" class="btn btn-info btn-xs" onclick="submitBatch('true')">提交批次(F4)</button>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">流水信息</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="input_vch_no"/>
                                <input type="hidden" class="form-control" id="input_trade_id"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal inmodal fade" id="inputTypeModel" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title">凭证类型</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="input_type" required class="col-sm-2 control-label">请选择</label>
                        <div class="row">
                            <div class="col-lg-6">
                                <div class="input-group">
                                    <input type="text" class="form-control" id="input_type">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="submitInput()">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: viewer-js" />
<th:block th:include="include :: bootstrap-table-reorder-js" />
<th:block th:include="include :: bootstrap-suggest-js" />
<script th:inline="javascript">
    var prefix = '/sdi/input';
    var inputFlag = [[${@permission.hasPermi('sdi:manualInput:input')}]];
    var arcId = [[${arcId}]];
    var sdiDocDefineList;
    var sdiPaeaArcInput;
    var sdiImageIndex = 0;
    var originSdiImageListData;
    var originPaeaDsTradeListData;
    var tradeMap = new HashMap();
    var vchNoIdMap = new HashMap();
    var sdiDocDefineMap = new HashMap();
    var matchedTradeMap = new HashMap();
    var unMatchedTradeMap = new HashMap();
    var originSdiImageMap = new HashMap();
    var originPaeaDsTradeMap = new HashMap();
    var totalImageNum = 0;
    var viewer;

    $('#inputTypeModel').draggable();
    $('#input_vch_no').keydown(function(event){
        if (event.keyCode == 13){
            if(checkVchNoExists()){
                $('#inputTypeModel').modal();
            }
        }
    });
    $('#input_type').keydown(function(event){
        if (event.keyCode == 13){
            submitInput();
        }
    });
    $(function () {
        //初始化影像控件
        initImageArea();
        //初始化页面数据
        init();
        //初始化影像
        initImage();
        if(arcId && arcId!='') {
            setTimeout('obtainTaskByid("'+arcId+'")',100);
        }
    })

    function init() {
        sdiPaeaArcInput = null;
        sdiImageIndex = 0;
        originSdiImageListData = null;
        originPaeaDsTradeListData = null;
        tradeMap.clear();
        vchNoIdMap.clear();
        matchedTradeMap.clear();
        unMatchedTradeMap.clear();
        originSdiImageMap.clear();
        originPaeaDsTradeMap.clear();
        totalImageNum = 0;
        //初始凭证类型信息
        initDocType();
        //初始化影像列表
        initImageList();
        //初始化流水列表
        initPaeaDsTradeList();
        //初始化补录信息
        /*if(inputFlag!='hidden') {
            initInputArea();
        }*/
        g_isRealSubmit = false;
        $('#input_vch_no').focus();
    }

    //初始凭证类型信息
    function initDocType(){
        sdiDocDefineList = [[${sdiDocDefineList}]];
        $.each(sdiDocDefineList,function(i,sdiDocDefine){
            sdiDocDefineMap.put(sdiDocDefine.code,sdiDocDefine);
        })
        $("#input_type").bsSuggest({
            indexId: 0,
            indexKey: 0,
            data: JSON.parse(JSON.stringify({'value':sdiDocDefineList}))
        });
    }

    //初始化影像列表
    function initImageList(){
        $('#arcCode').text('');
        totalImageNum = 0;
        $("#sdiImageInput-table").bootstrapTable('removeAll');
        $("#sdiImageInput-table").bootstrapTable(sdiImageDetailOptions);
    }

    //初始化流水列表
    function initPaeaDsTradeList(){
        $("#paeaDsTradeInput-table").bootstrapTable('removeAll');
        $("#paeaDsTradeInput-table").bootstrapTable(paeaDsTradeDetailOptions);
    }

    //初始化影像
    function initImage() {
        var imgUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584958911886&di=47b6a313b01424ad2593609115dbd145&imgtype=0&src=http%3A%2F%2Fbbsfiles.vivo.com.cn%2Fvivobbs%2Fattachment%2Fforum%2F201507%2F31%2F161843qkvv4oq21nrvtjui.jpg";
        updateImage(imgUrl);
    }
    //初始化影像显示
    function initImageArea() {
        //加载一张默认图片
        $('#imageBox').append('<img name= "img'+'demo'+'" id="imgArea" src="" alt="示例图片">');
        viewer = new Viewer(document.getElementById('imageBox'),{
            inline: true,
            navbar: false,
            toolbar: false,
            title: false,
            button: false
        });
    }

    //动态加载图片
    function updateImage(imgUrl){
        $('#imgArea').attr("src", imgUrl);
        viewer.update();
    }

    //影像列表
    var sdiImageDetailOptions = {
        reorderableRows: true,
        useRowAttrFunc: true,
        clickToSelect: true,
        onClickRow: function(row,$element) {
            sdiImageIndex = $element.data('index');
        },
        onCheck:function(row,$element){
            var imgUrl = "http://"+sdiPaeaArcInput.imageUrlPrefix+row.sdifilepath;
            updateImage(imgUrl);
            updateTradeTable(false);
            sdiImageIndex = $element.data('index');
        },
        onReorderRow: function (data) {
            //当拖拽结束后，data为整个表格的数据
            console.log('拖拽结束' + JSON.stringify(data))
            return false;
        },
        columns: [{
                radio: true,
                formatter: function (value, row, index) {
                    if(index==0){
                        return {
                            checked : true
                        };
                    }
                }
            },
            {
                field : 'seq',
                title : '序号',
                formatter: function (value, row, index) {
                    return row.indexNo;
                }
            },
            {
                field : 'vchNos',
                title : '流水号',
                formatter: function(value, row, index) {
                    var sdiImageRelTradeList = row.sdiImageRelTradeList;
                    var descVchNos = [];
                    $.each(sdiImageRelTradeList,function(i,sdiImageRelTrade) {
                        descVchNos.push(sdiImageRelTrade.vchNo);
                    })
                    return $.table.tooltip(descVchNos.join(","));
                }
            },
            {
                field : 'type',
                title : '类型',
                formatter: function(value, row, index) {
                    return mainType(value);
                }
            },
            {
                field : 'type',
                title : '票据类型',
                formatter: function(value, row, index) {
                    if(value!=null &&value!=""){
                        return sdiDocDefineMap.get(value).name;
                    }
                    return value;
                }
            },
            {
                field : 'txnAmt',
                title : '金额',
                formatter: function(value, row, index) {
                    var sdiImageRelTradeList = row.sdiImageRelTradeList;
                    var descVchNos = [];
                    $.each(sdiImageRelTradeList,function(i,sdiImageRelTrade) {
                        descVchNos.push(sdiImageRelTrade.txnAmt);
                    })
                    return $.table.tooltip(descVchNos.join(","));
                }
            },
            {
                field : 'status',
                title : '状态',
                formatter: function(value,row,index){
                    return getImageStatusDesc(value);
                }
            }]
    };

    //流水列表
    var paeaDsTradeDetailOptions = {
        onClickRow: function(row,$element) {
            if(row.status!='1' && row.status!='2'){
                $('#input_vch_no').val(row.vchNo);
                $('#input_trade_id').val(row.id);
                $('#input_vch_no').focus();
                moveEnd(document.getElementById('input_vch_no'));
            }
        },
        columns: [{
            checkbox: true
        },
            {
                field : 'id',
                title : '主键',
                visible: false
            },
            {
                field : 'seq',
                title : '序号',
                formatter: function (value, row, index) {
                    return row.indexNo;
                }
            },
            {
                field : 'vchNo',
                title : '流水号'
            },
            {
                field : 'accNo',
                title : '账号'
            },
            {
                field : 'txnAmt',
                title : '金额'
            },
            {
                field : 'txnCde',
                title : '交易码'
            },
            {
                field : 'ccyCode',
                title : '币种'
            },
            {
                field : 'status',
                title : '状态',
                formatter: function (value,row,index) {
                    return getTradeStatusDesc(value);
                }
            }]
    };

    //获取任务
    function obtainTask(type) {
        if(sdiPaeaArcInput!=null && changed()) {
            if (!confirm("当前批次未保存/提交，确定要选择批次吗?")) {
                return;
            }
        }
        if(type=='random'){
            setTimeout('obtainTaskByid()',100);
        } else if (type=='paused'){
            g_arc_id = window.showModalDialog(prefix+'/pausedTask/manualInput',window,'dialogWidth=900px; dialogHeight=600px; help=no; status=no; scroll=yes; resizable=no;');
            if(g_arc_id != null)
            {
                setTimeout('obtainTaskByid("'+g_arc_id+'")',100);
            }
        }
    }

    var g_isRealSubmit = false;
    //初始化流水信息
    function initTradeList(paeaDsTradeInputList) {
        $.each(paeaDsTradeInputList,function(i,paeaDsTradeInput){
            tradeMap.put(paeaDsTradeInput.id,paeaDsTradeInput);
            vchNoIdMap.put(paeaDsTradeInput.vchNo,paeaDsTradeInput.id);
            if(paeaDsTradeInput.imageId="" || paeaDsTradeInput.imageId==null) {
                unMatchedTradeMap.put(paeaDsTradeInput.id,paeaDsTradeInput);
            } else {
                matchedTradeMap.put(paeaDsTradeInput.id,paeaDsTradeInput);
            }
        })
    }

    function obtainTaskByid(arcId) {
        if (g_isRealSubmit ==true) return false;
        g_isRealSubmit = true;
        var url = prefix+"/manualInputBatchInfo";
        if(arcId){
            url= url+"/"+arcId;
        } else {
            url= url+"/random";
        }
        $.operate.get(url,function(result){
            var manualInputBatchInfo = result.data;
            if(!manualInputBatchInfo || !manualInputBatchInfo.sdiPaeaArcInput ){
                $.modal.msgWarning('当前没有任务！');
                g_isRealSubmit = false;
                return;
            }
            var sdiImageInputList = manualInputBatchInfo.sdiImageInputList;
            if(manualInputBatchInfo.sdiPaeaArcInput!=null){
                if(sdiImageInputList==null || sdiImageInputList.length==0) {
                    $.modal.msgError("批次无影像记录，无法补录");
                    g_isRealSubmit = false;
                    return;
                } else {
                    init();
                }
            }
            sdiPaeaArcInput = manualInputBatchInfo.sdiPaeaArcInput;
            //赋值批次信息
            $('#arcCode').text(sdiPaeaArcInput.code);

            //显示流水搜索信息
            var paeaDsTradeInputList = manualInputBatchInfo.paeaDsTradeInputList;
            if(paeaDsTradeInputList!=null && paeaDsTradeInputList.length>0){
                $("#paeaDsTradeInput-table").bootstrapTable('load',paeaDsTradeInputList);
                initTradeList(paeaDsTradeInputList);
            }
            //初始化流水信息
            originPaeaDsTradeListData = JSON.parse(JSON.stringify(paeaDsTradeInputList));
            $.each(originPaeaDsTradeListData,function(i,originPaeaDsTrade){
                originPaeaDsTradeMap.put(originPaeaDsTrade.id,originPaeaDsTrade);
            })
            //显示影像信息
            originSdiImageListData = JSON.parse(JSON.stringify(sdiImageInputList));
            $.each(originSdiImageListData,function(i,originSdiImage){
                originSdiImage.indexNo = i+1;
                originSdiImageMap.put(originSdiImage.id,originSdiImage);
            })
            $("#sdiImageInput-table").bootstrapTable('load',sdiImageInputList);
            $("#sdiImageInput-table").bootstrapTable('check',0);
            totalImageNum = sdiImageInputList.length;

            updateTradeNumDisplay();
            g_isRealSubmit = false;
        })
    }

    //提交补录信息
    function submitInput(){

        //获取当前选择的影像记录
        if(sdiImageIndex!=0){
            $("#sdiImageInput-table").bootstrapTable('uncheck',0);
        }
        var type = $('#input_type').val();
        var image = $("#sdiImageInput-table").bootstrapTable('getSelections')[0];
        var tradeId = $('#input_trade_id').val();
        if(!tradeId || tradeId==''){
            if(unMatchedTradeMap.size()>0) {
                tradeId = unMatchedTradeMap.keySet()[0];
            }
            // $.modal.msgWarning('流水为空，无法勾对');
            // $('#input_vch_no').focus();
            // return false;
        }
        if(!type){
            $.modal.msgWarning('凭证类型为空，无法勾对');
            $('#input_type').focus();
            return false;
        }

        image = reBuildImageRelTrade(image,tradeId);
        image.type = type;
        image.status = '2';
        $("#sdiImageInput-table").bootstrapTable('updateRow',{index:sdiImageIndex,row:image});
        if(sdiImageIndex+1 < totalImageNum){
            $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex+1);
        } else {
            $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex);
        }
        $('#input_vch_no').val('');
        $('#input_trade_id').val('');
        $('#input_type').val('');
        updateTradeNumDisplay();
        updateTradeTable(true);
        $('#inputTypeModel').modal('hide');
        $('#input_vch_no').focus();
    }


    function checkVchNoExists(){
        var vchNo = $('#input_vch_no').val();
        if(!vchNoIdMap.containsKey(vchNo)){
            $.modal.msgError('输入的流水不存在！');
            return false;
        } else {
            var tradeId = vchNoIdMap.get(vchNo);
            if(matchedTradeMap.containsKey(tradeId)){
                $.modal.msgError('输入的流水已勾对！');
                return false;
            } else {
                $('#input_trade_id').val(tradeId);
                return true;
            }
        }

    }
    function reBuildImageRelTrade(image,tradeId) {
        image = cancelOldImageRelTrade(image);
        var paeaDsTradeInput = tradeMap.get(tradeId);
        paeaDsTradeInput.status='2';

        var sdiImageRelTrade = {};
        sdiImageRelTrade.id = paeaDsTradeInput.id;
        sdiImageRelTrade.txnAmt = paeaDsTradeInput.txnAmt;
        sdiImageRelTrade.vchNo = paeaDsTradeInput.vchNo;
        sdiImageRelTrade.status = paeaDsTradeInput.status;
        image.sdiImageRelTradeList.push(sdiImageRelTrade);
        paeaDsTradeInput.imageId = image.id;
        var compTrade = unMatchedTradeMap.get(tradeId);
        matchedTradeMap.put(tradeId,compTrade);
        unMatchedTradeMap.remove(tradeId);
        return image;
    }

    function cancelInput(){
        if(sdiImageIndex!=0){
            $("#sdiImageInput-table").bootstrapTable('uncheck',0);
        }
        //获取当前选择的影像记录
        var image = $("#sdiImageInput-table").bootstrapTable('getSelections')[0];
        image = cancelOldImageRelTrade(image)
        image.status = '0';
        $("#sdiImageInput-table").bootstrapTable('updateRow',{index:sdiImageIndex,row:image});
        $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex);
        updateTradeNumDisplay();
        updateTradeTable(true);

    }

    function cancelOldImageRelTrade(image) {
        var oldSdiImageRelTradeList = image.sdiImageRelTradeList;
        $.each(oldSdiImageRelTradeList,function(i,oldSdiImageRelTrade) {
            var tradeId = oldSdiImageRelTrade.id;
            var compTrade = matchedTradeMap.get(tradeId);
            compTrade.status = '0';
            originPaeaDsTradeMap.get(tradeId).status = '0';
            unMatchedTradeMap.put(tradeId,compTrade);
            matchedTradeMap.remove(tradeId);
            tradeMap.get(tradeId).imageId = '';
        })
        image.sdiImageRelTradeList = [];
        image.type = '';
        return image;
    }

    //提交批次
    function submitBatch(flag) {
        var canSubmit = true;
        var sdiImageTableData= $('#sdiImageInput-table').bootstrapTable('getData',{useCurrentPage:false,includeHiddenRows:false});
        // 判断第一张影像如果为副票，禁止保存提交
        if(sdiImageTableData[0].type && sdiImageTableData[0].type.substr(0,1)=='2'){
            $.modal.msgWarning("第一张影像不能为副票，禁止提交！");
            canSubmit = false;
        }

        if(canSubmit){
            //获取页面发生变化的影像列表
            var changedSdiImageList = [];
            $.each(sdiImageTableData,function(i,row){
                row.indexNo = i+1;
                var equals = deepCompare(row,originSdiImageMap.get(row.id));
                if(!equals){
                    changedSdiImageList.push(row);
                    originSdiImageMap.put(row.id,row);
                }
            })
            //获取页面发生变化的流水列表
            var paeaDsTradeData = tradeMap.values();
            var changedPaeaDsTradeList = [];
            $.each(paeaDsTradeData,function (i,row) {
                var equals = deepCompare(row,originPaeaDsTradeMap.get(row.id));
                if(!equals){
                    changedPaeaDsTradeList.push(row);
                    originPaeaDsTradeMap.put(row.id,row);
                }
            })
            if(changedSdiImageList.length==0 && changedPaeaDsTradeList.length == 0 && flag=='false') {
                $.modal.msgWarning("没有改变任何信息，无需保存！");
                return;
            }
            if(flag=='true'){
                // 检查未勾对流水数量
                if(unMatchedTradeMap.size()!=0){
                    $.modal.msgWarning("还有流水未勾对，无法提交！");
                    return;
                }
            }
            g_isRealSubmit = true;
            $.ajax({
                type : "post",
                url : prefix+"/submitManualInputBatch",
                data : JSON.stringify({
                    isCommit: flag,
                    sdiPaeaArcInput: sdiPaeaArcInput,
                    sdiImageInputList: changedSdiImageList,
                    paeaDsTradeInputList: changedPaeaDsTradeList}),
                contentType:"application/json",
                dataType : "json",
                success : function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.alertSuccess(result.msg)
                    } else if (result.code == web_status.WARNING) {
                        $.modal.alertWarning(result.msg)
                        g_isRealSubmit =false;
                        return false;
                    } else {
                        $.modal.alertError(result.msg);
                        g_isRealSubmit =false;
                        return false;
                    }
                    if(flag=='false'){
                        $('#sdiImageInput-table').bootstrapTable('load',sdiImageTableData);
                        $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex);
                        originSdiImageListData = JSON.parse(JSON.stringify(sdiImageTableData));
                        $.each(originSdiImageListData,function(i,originSdiImage){
                            originSdiImage.indexNo = i+1;
                            originSdiImageMap.put(originSdiImage.id,originSdiImage);
                        })
                        originPaeaDsTradeListData = JSON.parse(JSON.stringify(paeaDsTradeData));
                        $.each(originPaeaDsTradeListData,function(i,originPaeaDsTrade){
                            originPaeaDsTradeMap.put(originPaeaDsTrade.id,originPaeaDsTrade);
                        })
                    } else {
                        init();
                        updateTradeNumDisplay();
                    }
                    g_isRealSubmit = false;
                }
            });
        }
    }

    //检查批次是否发生补录变化
    function changed() {
        var changed = false;
        var sdiImageTableData= $('#sdiImageInput-table').bootstrapTable('getData',{useCurrentPage:false,includeHiddenRows:false});
        $.each(sdiImageTableData,function(i,row){
            row.indexNo = i+1;
            var equals = deepCompare(row,originSdiImageMap.get(row.id));
            if(!equals){
                changed = true;
            }
        })
        var paeaDsTradeData = tradeMap.values();
        $.each(paeaDsTradeData,function (i,row) {
            var equals = deepCompare(row,originPaeaDsTradeMap.get(row.id));
            if(!equals){
                changed = true;
            }
        })
        return changed;
    }
    // 标记副票
    function signAttachedPage(){
        if(sdiImageIndex!=0){
            $("#sdiImageInput-table").bootstrapTable('uncheck',0);
        } else {
            $.modal.msgWarning("第一张凭证无法勾对为副票！");
            return;
        }
        var image = $("#sdiImageInput-table").bootstrapTable('getSelections')[0];
        image = cancelOldImageRelTrade(image);
        image.type="2999";
        image.status = '2';
        $("#sdiImageInput-table").bootstrapTable('updateRow',{index:sdiImageIndex,row:image});
        if(sdiImageIndex+1 < totalImageNum){
            $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex+1);
        } else {
            $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex);
        }
        $('#input_vch_no').focus();
        updateTradeNumDisplay();
    }

    // 标记其他
    function signOtherPage(){
        if(sdiImageIndex!=0) {
            $("#sdiImageInput-table").bootstrapTable('uncheck', 0);
        }
        var image = $("#sdiImageInput-table").bootstrapTable('getSelections')[0];
        image = cancelOldImageRelTrade(image);
        image.type="3001";
        image.status = '2';
        $("#sdiImageInput-table").bootstrapTable('updateRow',{index:sdiImageIndex,row:image});
        if(sdiImageIndex+1 < totalImageNum){
            $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex+1);
        } else {
            $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex);
        }
        $('#input_vch_no').focus();
        updateTradeNumDisplay();
    }
    function doKeyDown(keyCode) {
        if(inputFlag!='hidden'){
            // 112 F1
            if (keyCode==112) {
                if(document.all.taskToolBar.style.display=="none")
                {
                    submitInput();
                }
                else
                {
                    document.all.taskToolBar.style.display='none';
                    setTimeout('obtainTask()',100);
                }
            }

            // 113 F2
            if (keyCode==113) {
                obtainPausedTask();
            }
            //34 PageDown
            if (keyCode==34) {
                signAttachedPage();
            }
            // 114 F3
            if (keyCode==114) {
                submitBatch('false');
            }
            //106 小键盘*号
            if (keyCode==106) {
                signOtherPage();
            }
            // 115 F4
            if (keyCode==115) {
                submitBatch('true');
            }
            // 116 F5  111 小键盘/号
            if (keyCode==116) {
                createError()
            }
            // 117 F6
            if (keyCode==117) {

            }
            // 118 F7
            if (keyCode==118) {
                signLoseBill();
            }
            // 119 F8  109  小键盘-号
            if (keyCode==109) {
                cancelMatch();
            }
            // 120 F9
            if (keyCode==120) {
                pauseTask();
            }
            // 121 F10  110 小键盘.号
            if (keyCode==110) {
                viewBack();
            }
            //left<-上移
            /*if(keyCode==38){
                if(document.getElementById("backDiv2").style.display=="none"){
                    if(document.getElementById("acctNo")!=document.activeElement) {
                        showUpItem();
                    }
                }

            }
            //right->下移
            if(keyCode==40){
                if(document.getElementById("backDiv2").style.display=="none"){
                    if(document.getElementById("acctNo")!=document.activeElement) {
                        showNextItem();
                    }
                }
            }*/
            //向上移动图片 W
            /*if(keyCode==87){
                if(document.getElementById("backDiv2").style.display=="none"){
                        upImage();
                }
            }	*/

            //向下移动图片 S
            /*if(keyCode==83){
                if(document.getElementById("backDiv2").style.display=="none"){
                        downImage();
                }
            }	*/
        }

        //截屏
        if(keyCode==122){
            add();
        }
        //缩小影像
        if(keyCode==188) {
            zoomIn();
        }
        //放大影像
        if(keyCode==190) {
            zoomOut();
        }
        //旋转影像
        if(keyCode==191) {
            rotation();
        }
        //最佳影像
        if(keyCode==39) {
            toggle();
        }
        //复制影像
        if(keyCode==122) {
            copyImage();
        }
        //移动影像:pageup/m
      /*  if(keyCode==_KEY.PAGEUP || keyCode == 77) {
            if(document.getElementById("acctNo")!=document.activeElement) {
                exchange();
            }
        }*/
        //正面反面
        if(keyCode==220){
            viewBack();
        }
        //删除:delete/d
        if(keyCode==46 || keyCode==68){
            deleteCurrentPage();
        }
        //全部标注 ：a
        if(keyCode==65){
            signAllRestPage();
        }
        if(keyCode == 81) {
            switchAutoJump();
            keyCode = 0;
        }
        if(keyCode == 72) {
            querySdiPaeaArcHistory();
        }
        //add by lisc 2014-03-28 按ALT切换8888流水和未勾兑流水列表
        if(keyCode == 16){
            if(!document.all.cusTradeBT.checked){
                document.all.cusTradeBT.checked='true';
                display8888Trade();
                //document.all.cusTradeBT.focus();
                document.getElementById('vchNo').focus();
            }else{
                document.all.notOkTradeBT.checked='true';
                displayAllNotOkTrade();
                //document.all.notOkTradeBT.focus();
                document.getElementById('vchNo').focus();
            }
        }
    }

    function zoomIn() {
        viewer.zoom(0.1);
    }

    function zoomOut() {
        viewer.zoom(-0.1);
    }

    function toggle() {
        viewer.toggle();
    }

    function createError() {
        if(changed()){
            $.modal.msgWarning("请先保存批次再登记缺票！");
            return;
        }
        var tradeTable = $("#paeaDsTradeInput-table");
        var rows = tradeTable.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条流水");
            return;
        }
        var canCreate  = true;
        var ids = [];
        $.each(rows,function(i,row){
            console.log(JSON.stringify(row));

            if(row.status=='1' || row.status=='2'){
                $.modal.msgWarning('已勾对流水：'+row.vchNo+',无法登记缺票！');
                canCreate = false;
                return false;
            }
            if(row.status=='3'){
                $.modal.msgWarning('已登记缺票流水：'+row.vchNo+',无法重复登记！');
                canCreate = false;
                return false;
            }
            ids.push(row.id);
        })
        if(canCreate){
            $.modal.confirm("确认要将选中的" + rows.length + "条流水登记缺票并创建差错吗?", function() {
                var url = prefix+'/lostVoucherRegister';
                var data = { "ids": ids.join(",") };
                $.operate.post(url,data,function() {
                    $.each(rows,function(i,row){
                        row.status = '3';
                    })
                    updateTradeTable(true);
                })
            });
        }
    }

    //选择显示流水
    $("input[name='tradeFilter']").click(function(){
        updateTradeTable(true);
    });

    //更新流水列表
    function updateTradeTable(flag){
        var radioValue =  $("input[name='tradeFilter']:checked").val();
        var tradeTable = $("#paeaDsTradeInput-table");
        tradeTable.bootstrapTable('uncheckAll');
        tradeTable.bootstrapTable('removeAll');
        if(radioValue=='0'){//全部流水
            tradeTable.bootstrapTable('load',tradeMap.values());
        } else if (radioValue=='1') {//未勾对流水
            tradeTable.bootstrapTable('load',unMatchedTradeMap.values());
        } else if (radioValue=='2') {//当前影像流水
            //获取当前选择的影像记录
            if(sdiImageIndex!=0){
                $("#sdiImageInput-table").bootstrapTable('uncheck',0);
            }
            var image = $("#sdiImageInput-table").bootstrapTable('getSelections')[0];
            if(image){
                var tradeTableData = JSON.parse(JSON.stringify(image.sdiImageRelTradeList));
                tradeTable.bootstrapTable('load',tradeTableData);
                if(flag){
                    $("#sdiImageInput-table").bootstrapTable('check',sdiImageIndex);
                }
            }

        }
    }
    //更新流水勾对显示信息
    function updateTradeNumDisplay() {
        $("#matchedTradeNum").text("已勾对：" + matchedTradeMap.size());
        $("#unMatchedTradeNum").text("未勾对：" + unMatchedTradeMap.size());
    }

    //查询当日一般补录量
    function manualInputWorkLoad() {
        $.operate.get(prefix+'/manualInputWorkload')
    }

    //影像类型显示
    function mainType(type) {
        if(type!=null && type!='') {
            var sdiDocDefine = sdiDocDefineMap.get(type);
            var mainPage = sdiDocDefine.mainPage;
            if(mainPage!=null) {
                if(mainPage=="1"){
                    return '<span class="badge badge-success">主票</span>'
                } else if(mainPage=="2"){
                    return '<span class="badge badge-default">附件</span>'
                } else if(mainPage=="3"){
                    return '<span class="badge badge-primary">其他</span>'
                }
                return '';
            }
        }
    }

    //影像状态中文描述
    function getImageStatusDesc(status) {
        if (status == '0' || status == '') {
            return '<code>未勾对</code>';
        } else if (status == '1') {
            return '自动勾对';
        } else if (status == '2') {
            return '手工勾对';
        } else if (status == '-1') {
            return '<code>已删除</code>';
        } else if (status == '9') {
            return '<code>补扫票</code>';
        }
        return '<code>未勾对</code>';
    }

    //流水状态中文描述
    function getTradeStatusDesc(status) {
        if (status == 0) {
            return '<span class="badge badge-warning">未勾对</span>';
        }
        else if (status == 1) {
            return '<span class="badge badge-primary">已勾</span>';
        }
        else if (status == 2) {
            return '<span class="badge badge-primary">已勾</span>';
        }
        else if(status==3){
            return '<span class="badge badge-danger">补票</span>';
        } else {
            return '<span class="badge badge-primary">已勾</span>';
        }
    }
</script>
</body>
</html>